package com.xiangb.springbootjpa.controller;

import com.xiangb.springbootjpa.common.PageVO;
import com.xiangb.springbootjpa.config.PersonProperties;
import com.xiangb.springbootjpa.entity.Person;
import com.xiangb.springbootjpa.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * Description: <br>
 *
 * @author: XB
 * @date: 2020/12/22 14:54
 */
@Controller
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private PersonProperties personProperties;

    @Autowired
    private PersonService personService;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    @ResponseBody
    public String say(){
        return "姓名：" + personProperties.getName() + " 年龄：" + personProperties.getAge();
    }

    /**
     * 分页查询列表
     * @param name
     * @param age
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/page",method = {RequestMethod.GET,RequestMethod.POST})
    public String personUpdate(ModelMap modelMap, @RequestParam(value = "name",required = false) String name,
                                       @RequestParam(value = "age",required = false) Integer age,
                                       @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageVO<Person> pageVO = personService.findByPage(pageNumber, pageSize, name, age);
        modelMap.addAttribute("datas", pageVO);
        return "index2";
    }
}
